package org.easyupdater.model;

import java.util.Set;

import org.osgi.framework.Version;

import com.thoughtworks.xstream.annotations.XStreamAlias;

@XStreamAlias("application")
public class ApplicationSpec implements Comparable<ApplicationSpec> {

	private String version;

	private String name;

	private String url;

	@XStreamAlias("bundles")
	private Set<BundleSpec> bundleSpecs;

	public ApplicationSpec() {
	}

	public Set<BundleSpec> getBundleSpecs() {
		return bundleSpecs;
	}

	public void setBundleSpecs(Set<BundleSpec> bundleSpecs) {
		this.bundleSpecs = bundleSpecs;
	}

	public String getVersion() {
		return version;
	}

	public void setVersion(String version) {
		this.version = version;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public int compareTo(ApplicationSpec otherApplicationSpec) {
		return new Version(version).compareTo(new Version(otherApplicationSpec.version));
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((url == null) ? 0 : url.hashCode());
		result = prime * result + ((version == null) ? 0 : version.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ApplicationSpec other = (ApplicationSpec) obj;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (url == null) {
			if (other.url != null)
				return false;
		} else if (!url.equals(other.url))
			return false;
		if (version == null) {
			if (other.version != null)
				return false;
		} else if (!version.equals(other.version))
			return false;
		return true;
	}
}
